//
//  GKStatusRetweetView.swift
//  MicroBlogFunc
//
//  Created by GK on 16/4/15.
//  Copyright © 2016年 GK. All rights reserved.
//

import UIKit

class GKStatusRetweetView: UIView {

    //被转发的微博
    var retweetStatus: GKStatus? {
        
        didSet{
            // 当别人设置被转发微博的内容,我们来设置相应的内容到view上面
            // 被转发的微博文本内容 = @被转发微博的用户名称: 被转发微博的文本内容
            let reTwName = retweetStatus?.user?.screen_name ?? "被转发的微博用户名称"
            let reTwText = retweetStatus?.text
            
            let reTwContent = "@\(reTwName):\(reTwText)"
            lblRetweet.text = reTwContent
            reTweetPicView.picURLs = retweetStatus?.storePicURLs
            reTweetPicView.snp_updateConstraints { (make) in
                let margin = retweetStatus?.storePicURLs == nil ? 0 : GKStatusCellMargin
                make.bottom.equalTo(margin)
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        prepareUI()
        self.backgroundColor = UIColor.blueColor()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    //更新UI
    private func prepareUI(){
        
//        //添加控件
        addSubview(btnBackground)
        addSubview(lblRetweet)
        addSubview(reTweetPicView)
        
        //添加约束
        btnBackground.snp_makeConstraints { (make) in
            make.edges.equalTo(self)
        }
        // 被转发的微博文本内容
        lblRetweet.snp_makeConstraints { (make) in
            make.leading.top.equalTo(self).offset(GKStatusCellMargin)
            make.trailing.equalTo(self).offset( -GKStatusCellMargin)
        }
        
        //配图view
        reTweetPicView.snp_makeConstraints { (make) in
            make.leading.equalTo(self).offset(GKStatusCellMargin)
            make.top.equalTo(lblRetweet.snp_bottom).offset(GKStatusCellMargin)
            make.size.equalTo(CGSizeZero)
        }
        
        // 添加底部约束让被转发的view的底部参照被转发的配图视图
        self.snp_makeConstraints { (make) in
            make.bottom.equalTo(reTweetPicView).offset(GKStatusCellMargin)
        }
        
    }
    
    //懒加载控件
    //背景图片,因为有高亮效果,所以用按钮
    private lazy var btnBackground:UIButton = {
       
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "retweet_background"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "retweet_background_highlighted"), forState: UIControlState.Highlighted)
        
        return btn
    }()
    //转发内容
    private lazy var lblRetweet: UILabel = {
       
        let lbl = UILabel(fontSize: 15, textColor: UIColor.darkGrayColor())
        lbl.numberOfLines = 0
        return lbl
    }()
    //转发的图片
    private lazy var reTweetPicView: GKStatusPictureView = GKStatusPictureView()
    
}
